package com.ssd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssd.entity.operation.dto.AssginMenuDto;
import com.ssd.entity.operation.entity.SysMenu;
import com.ssd.entity.operation.entity.SysRoleMenu;
import com.ssd.mapper.SysMenuMapper;
import com.ssd.mapper.SysRoleMenuMapper;
import com.ssd.service.SysMenuService;
import com.ssd.service.SysRoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 角色菜单表 服务实现类
 * </p>
 *
 */
@Service
@Slf4j
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysMenuService sysMenuService;
    //1 查询所有菜单 和 查询角色分配过菜单id列表
    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {
        // 查询所有菜单
        List<SysMenu> sysMenuList = sysMenuService.findNodes();

        // 询角色分配过菜单id列表
        List<Long> roleMenuIds = lambdaQuery().eq(SysRoleMenu::getRoleId, roleId).select(SysRoleMenu::getMenuId)
                .list().stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>();
        map.put("sysMenuList",sysMenuList);
        map.put("roleMenuIds",roleMenuIds);
        return map;
    }

    //分配菜单
    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        //删除角色分配菜单数据
        lambdaUpdate().eq(SysRoleMenu::getRoleId,assginMenuDto.getRoleId()).remove();

        //保存分配数据
        List<Map<String, Number>> menuInfo = assginMenuDto.getMenuIdList();
        if(menuInfo != null && menuInfo.size()>0) { //角色分配了菜单
            sysRoleMenuMapper.doAssign(assginMenuDto);
        }
    }

}
